package com.sui.bigdata.flink.sql.core.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.flink.calcite.shaded.com.google.common.collect.Maps;
import org.apache.http.entity.StringEntity;
import com.feidee.fdcommon.model.StringResponse;
import com.feidee.fdcommon.util.HttpUtil;
import org.apache.http.client.methods.HttpPost;
import org.apache.log4j.Logger;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @author YongChen
 * @date 2019/12/11 12:15
 * @description
 * @email yong_chen@sui.com
 */
public class SendMsgUtils {
    private static Logger logger = Logger.getLogger(SendMsgUtils.class);

    public static  void sendMsg (String url,String jobNmae,String email,String appStatus,String trackUrl) throws  Exception{

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String curTime = df.format(new Date(System.currentTimeMillis()));
        String alarmInfo ="实时标签-Flink task "+ jobNmae+ " change to "+ appStatus +" at "+ curTime +". Please " +email +" click "+ trackUrl +" for more details!";
        Map<String,String> body = Maps.newHashMap();
        body.put("text",alarmInfo);
        StringEntity stringEntity = new StringEntity(JSONObject.toJSONString(body), "UTF-8");
        stringEntity.setContentEncoding("UTF-8");
        stringEntity.setContentType("application/json");

        HttpPost httpPost=new HttpPost(url);
        httpPost.setEntity(stringEntity);

        StringResponse response = HttpUtil.post(httpPost);
        if ("success".equals(response.getResponseBody())){
            logger.info(" send msg succeed.");
        }else {
            logger.info(" send msg fail.");
        }
    }
}
